package service;

import com.sun.org.apache.regexp.internal.RE;
import common.Pagination;
import dao.CroPurchaseDao;
import entity.CroCustomer;
import entity.CroPurchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroPurchaseVo;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin on 2017/5/7.
 */
@Service
public class CroPurchaseService {

    @Autowired
    private CroPurchaseDao croPurchaseDao;

    /**
     * 添加订单
     *
     * @return 返回订单Id
     */
    @Transactional(readOnly = false)
    public String addPurchase(CroPurchase purchase) {
        String Id = null;
        try {
            Id = (String) croPurchaseDao.save(purchase);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return Id;
    }

    /**
     * 更新订单信息
     *
     * @return boolean
     */
    @Transactional(readOnly = false)
    public boolean updatePurchase(CroPurchase purchase) {
        try {
            croPurchaseDao.update(purchase);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 修改订单状态
     * @return boolean
     */
    @Transactional(readOnly = false)
    public boolean donePurchase(String id) {
        CroPurchase purchase = croPurchaseDao.getById(id);
        purchase.setOrderStatus("4");
        try {
            croPurchaseDao.update(purchase);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 通过id查询订单并转型
     *
     * @param id 订单id
     * @return purchase
     */
    @Transactional(readOnly = true)
    public CroPurchaseVo findByIdExt(String id) {
        String hql = "from CroPurchase where id=? and order_flag=1";
        return (CroPurchaseVo) croPurchaseDao.findByHqlExt(hql, CroPurchaseVo.class, id).get(0);
    }

    /**
     * 通过id查询订单不转型
     *
     * @param id 订单id
     * @return purchase
     */
    @Transactional(readOnly = true)
    public CroPurchase findById(String id) {
        return croPurchaseDao.getById(id);
    }

    /**
     * 返回所有的订单信息
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public List<CroPurchaseVo> lists() {
        StringBuilder hql = new StringBuilder();
        hql.append("from CroPurchase where order_flag=1");
        return croPurchaseDao.findByHqlExt(hql, CroPurchaseVo.class);
    }

    /**
     * 返回分页的订单信息
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public Pagination<CroPurchaseVo> listsPage(int index, int size) {
        StringBuilder hql = new StringBuilder();
        hql.append("from CroPurchase where order_flag=1 order by orderdate desc");
        Pagination<CroPurchaseVo> page;
        page = croPurchaseDao.findPagination(hql,index,size);
        try {
            page.setItems(croPurchaseDao.transEntity(page.getItems(), CroPurchaseVo.class));
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 通过多个属性查找订单
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public List<CroPurchaseVo> listByProperty(Map<String, Object> maps) {

        List list = croPurchaseDao.findByProperty(maps);
        List<CroPurchaseVo> purchaseVoList = new ArrayList<>();
        try {
            purchaseVoList = croPurchaseDao.transEntity(list, CroPurchaseVo.class);
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return purchaseVoList;
    }


    /**
     * 通过多个属性查找订单并分页
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public Pagination<CroPurchaseVo> listPageByProperty(Map<String, Object> maps,int index,int size) {

        Pagination<CroPurchaseVo> page;
        maps.put("order_flag", "1");
        page = croPurchaseDao.findPaginationByPropertyInvert(maps,"orderdate",index,size);
        try {
            page.setItems(croPurchaseDao.transEntity(page.getItems(), CroPurchaseVo.class));
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 通过id[]批量删除订单
     *
     * @param ids 订单id[]
     * @return boolean
     * @apiNote 后台管理员删除功能
     */
    @Transactional(readOnly = false)
    public boolean delPurchase(String[] ids) {
        try {
            for (String id : ids) {
                croPurchaseDao.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 通过id删除订单（设置订单的删除状态）
     *
     * @param id 订单id
     * @return boolean
     * @apiNote 前台用户删除功能
     */
    @Transactional(readOnly = false)
    public boolean userDelPurchase(String id) {
        try {
            CroPurchase purchase = croPurchaseDao.getById(id);
            purchase.setDel_flag("1");
            croPurchaseDao.save(purchase);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据用户实体查找未下单的订单
     * */
    @Transactional(readOnly = true)
    public CroPurchase FindPurchaseByCus(String cusid){
        Map<String,Object> map=new HashMap<>();
        CroPurchase croPurchase=null;
        map.put("costomId",cusid);
        map.put("order_flag","0");
        try{
            croPurchase=croPurchaseDao.findByProperty(map).get(0);
        }catch (Exception e){
            croPurchase=null;
        }
        return croPurchase;
    }
}
